﻿using System.Data;
using System.Data.Common;

namespace Quest.Core.DataAccess
{
    /// <summary>
    /// 数据访问层基类
    /// </summary>
    public class ServerBase
    {
        private IServer Server { get; set; }
        /// <summary>
        /// 数据访问层基类
        /// </summary>
        public ServerBase()
        {
            Server = AccessProvider.Create();
        }
        /// <summary>
        /// 数据访问层基类
        /// </summary>
        /// <param name="name">连接字符串配置名称</param>
        public ServerBase(string name)
        {
            Server = AccessProvider.Create(name);
        }
        /// <summary>
        /// 创建新访问
        /// </summary>
        /// <param name="name">连接字符串配置名称</param>
        public void CreateNew(string name)
        {
            Server = AccessProvider.Create(name);
        }
        /// <summary>
        /// 创建新访问
        /// </summary>
        /// <param name="providerName">数据库类型</param>
        /// <param name="connectionString">连接字符串</param>
        public void CreateNew(string providerName, string connectionString)
        {
            Server = AccessProvider.Create(providerName, connectionString);
        }

        #region 增加参数  

        /// <summary>
        /// 增加输入参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbType"></param>
        /// <param name="value"></param>
        public void AddParameter(string name, DbType dbType, object value)
        {
            Server.AddParameter(name, dbType, value);
        }

        /// <summary>
        /// 增加输出参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbType"></param>
        /// <param name="size"></param>
        public void AddOutParameter(string name, DbType dbType, int size)
        {
            Server.AddOutParameter(name, dbType, size);
        }

        /// <summary>
        /// 增加返回参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbType"></param>
        public void AddReturnParameter(string name, DbType dbType)
        {
            Server.AddReturnParameter(name, dbType);
        }

        #endregion

        #region 数据库操作

        public DataSet ExecuteDataSet(string text, CommandType type = CommandType.Text)
        {
            return Server.ExecuteDataSet(text, type);
        }

        public DataTable ExecuteDataTable(string text, CommandType type = CommandType.Text)
        {
            return Server.ExecuteDataTable(text, type);
        }

        public DbDataReader ExecuteReader(string text, CommandType type = CommandType.Text)
        {
            return Server.ExecuteReader(text, type);
        }

        public int ExecuteNonQuery(string text, CommandType type = CommandType.Text)
        {
            return Server.ExecuteNonQuery(text, type);
        }

        public object ExecuteScalar(string text, CommandType type = CommandType.Text)
        {
            return Server.ExecuteScalar(text, type);
        }

        #endregion

        #region 执行事务

        /// <summary>
        /// 开始事物
        /// </summary>
        public void BeginTransaction()
        {
            Server.Open();
            Server.Transaction = Server.Connection.BeginTransaction();
            Server.Command.Transaction = Server.Transaction;
        }

        /// <summary>
        /// 关联事物
        /// </summary>
        public void RelationTransaction(ServerBase serverBase)
        {
            Server.ConnectionString = serverBase.Server.ConnectionString;
            Server.Connection = serverBase.Server.Transaction.Connection;
            Server.Command = serverBase.Server.Command;
            Server.Transaction = serverBase.Server.Transaction;
        }

        /// <summary>
        /// 提交事物
        /// </summary>
        public void Commit()
        {
            Server.Transaction.Commit();
            Server.Transaction = null;
            Server.Close();
        }

        /// <summary>
        /// 回滚事物
        /// </summary>
        public void RollBack()
        {
            Server.Transaction.Rollback();
            Server.Transaction = null;
            Server.Close();
        }

        #endregion
    }
}
